﻿using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AcDotNetTool;
using Autodesk.AutoCAD.Geometry;
namespace _13线型操作
{
    public class Class1
    {
        [CommandMethod("LineTypeDemo")]
        public void LineTypeDemo()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            ObjectId lineTypeId = this.LoadLineType(db, "CENTER");
            //ObjectId cirId = db.AddCircleModelSpace(new Point3d(10, 10, 0), 50);
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                //LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForRead);
                //if (lt.Has("0"))
                //{
                //    LayerTableRecord ltr = (LayerTableRecord)lt["0"].GetObject(OpenMode.ForWrite);
                //    ltr.LinetypeObjectId = lineTypeId;
                //}
                //Circle cir = (Circle)cirId.GetObject(OpenMode.ForWrite);
                ////cir.Linetype = "CENTER";
                //cir.LinetypeId = lineTypeId;
                //cir.ColorIndex = 1;
                DimStyleTable dst = (DimStyleTable)trans.GetObject(db.DimStyleTableId, OpenMode.ForRead);

                if (dst.Has("Standard"))
                {
                    DimStyleTableRecord dstr = (DimStyleTableRecord)dst["Standard"].GetObject(OpenMode.ForWrite);
                    dstr.Dimltype = lineTypeId;
                    db.SetDimstyleData(dstr);
                }
                trans.Commit();
            }
            
        }

        private ObjectId LoadLineType(Database db, string lineTypeName)
        {
            ObjectId lintTypeId = ObjectId.Null;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                //打开线型表
                LinetypeTable ltt = (LinetypeTable)trans.GetObject(db.LinetypeTableId, OpenMode.ForRead);
                //判断图形数据库是否已存在指定的线型
                if (!ltt.Has(lineTypeName))
                {
                    db.LoadLineTypeFile(lineTypeName, "acad.lin");
                }
                lintTypeId = ltt[lineTypeName];
                trans.Commit();
            }

            return lintTypeId;
        }
    }
}
